home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Enhancements / ImageConvert / ImageConvert.rexx < prev   
OS/2 REXX Batch file  |  1998-10-13  |  15KB  |  458 lines

  1. /* $VER: ImageConvert.rexx 1.8 (13.10.98) (C) by Martin Steigerwald
  2. */
  3.  
  4. OPTIONS RESULTS
  5.  
  6. PARSE ARG files
  7.  
  8. SIGNAL ON SYNTAX
  9. SIGNAL ON HALT
  10. SIGNAL ON BREAK_C
  11.  
  12. handle=0
  13. Initialize()
  14. ConfigDialog()
  15.  
  16. appmode=UPPER(appmode)
  17.  
  18. maxfiles=0
  19. DO UNTIL files=''
  20.     files=STRIP(files)
  21.     maxfiles=maxfiles+1
  22.     IF LEFT(files,1)=='"' THEN DO
  23.         PARSE VAR files '"' file.maxfiles '"' files
  24.     END
  25.     ELSE DO
  26.         PARSE VAR files file.maxfiles ' ' files
  27.     END /* IF */
  28. END /* DO */
  29.  
  30. IF reqmode=='opus' THEN DO
  31.     ADDRESS VALUE opusport
  32.     DOPUS PROGRESS 'name bar abort'
  33.     handle=RESULT
  34.     IF handle>0 THEN DO
  35.         DOPUS PROGRESS handle TITLE appname
  36.     END
  37. END
  38.  
  39. currentpath=PRAGMA('d')
  40.  
  41. DO i=1 TO maxfiles
  42.     IF reqmode=='reqtools' THEN DO
  43.         SAY 'Converting' file.i 'with' appmode 'to' destw 'x' desth '×' destcolors '...'
  44.     END
  45.     ELSE DO
  46.         IF handle>0 THEN DO
  47.             ADDRESS VALUE opusport
  48.             DOPUS PROGRESS handle NAME file.i
  49.             DOPUS PROGRESS handle ABORT
  50.             IF RESULT=1 THEN LEAVE
  51.             ADDRESS
  52.         END
  53.     END
  54.     file=file.i
  55.     IF POS(':', file)=0 THEN DO
  56.         file=currentpath'/'file
  57.     END
  58.     CALL Convert(destw desth destcolors destformat appmode file)
  59.     IF reqmode=='opus' & handle>0 THEN DO
  60.         ADDRESS VALUE opusport
  61.         DOPUS PROGRESS handle BAR maxfiles i
  62.         ADDRESS
  63.     END
  64. END
  65.  
  66. Cleanup()
  67. EXIT
  68.  
  69. syntax:
  70. SAY 'Error:' rc',' ERRORTEXT(rc) 'in line' sigl'.'
  71.  
  72. break_c:
  73. halt:
  74. Cleanup()
  75. EXIT
  76.  
  77. Cleanup:
  78. IF handle~=0 THEN DO
  79.     ADDRESS VALUE OPUSPORT
  80.     DOPUS PROGRESS handle OFF
  81.     ADDRESS
  82.     handle=0
  83. END
  84. RETURN 0
  85.  
  86. EXIT
  87.  
  88. convert:
  89.     PARSE ARG destw ' ' desth ' ' destcolors ' ' destformat ' 'appmode ' ' file
  90.  
  91.     IF EXISTS(file) THEN DO
  92.         destfile=newext(destformat file)
  93.         SELECT
  94.             WHEN appmode=='PPAINT' THEN DO
  95.                 IF ~SHOW('P', 'PPAINT') THEN DO
  96.                     ADDRESS COMMAND 'run >NIL: <NIL: PPaint:PPaint'
  97.                     ADDRESS COMMAND 'WaitForPort PPAINT'
  98.                 END /* IF */
  99.                 ADDRESS PPAINT
  100.                 IF destcolors>256 THEN DO
  101.                     destcolors=256
  102.                 END
  103.                 LockGUI
  104.                 Version REXX
  105.                 ver=RESULT
  106.                 LoadBrush 'FILE "'file'" FORCE QUIET NOPROGRESS'
  107.                 IF RC>0 THEN DO
  108.                    request('"'getstr(100, 'OK')'"' getstr(4, 'Could not load file »%s«!',file))
  109.                 END
  110.                 ELSE DO
  111.                     /* Shall I scale the picture? */
  112.                     IF destw>0 THEN DO
  113.                         IF ver>=8 THEN DO
  114.                             GetCurrentBrush
  115.                             currentbsh = RESULT
  116.                             custombsh = (WORD(currentbsh, 1) = 'BRUSH')
  117.                             IF custombsh THEN DO
  118.                                 SetBrushAttributes 'WIDTH' destw 'HEIGHT' desth 'COLORS' destcolors 'EXTENDPALETTE FORCE QUIET NOPROGRESS'
  119.                             END
  120.                             ELSE DO
  121.                                 request('"'getstr(100, 'OK')'"' getstr(4, 'Could not load file »%s«!',file))
  122.                             END /* IF custombsh */
  123.                         END
  124.                         ELSE DO
  125.                             SetBrushAttr 'WIDTH' destw 'HEIGHT' desth 'COLORS' destcolors 'FORCE QUIET NOPROGRESS'
  126.                         END /* version check */
  127.                     END
  128.                     SaveBrush 'FILE "'destfile'" FORCE QUIET NOPROGRESS FORMAT' destformat
  129.                 END /* LoadBrush check */
  130.                 FreeBrush FORCE
  131.                 UnlockGUI
  132.                 ADDRESS
  133.             END /* WHEN */
  134.             WHEN appmode=='ARTEFFECT' THEN DO
  135.                 IF ~SHOW('P', 'ArtEffect') THEN DO
  136.                     oldstack=PRAGMA('STACK', 30000)
  137.                     ADDRESS COMMAND 'run >NIL: <NIL: ArtEffect:ArtEffect'
  138.                     ADDRESS COMMAND 'WaitForPort ArtEffect'
  139.                     dummy=PRAGMA('STACK', oldstack)
  140.                 END /* IF */
  141.                 ADDRESS "ArtEffect"
  142.                 ds=UPPER(destformat)
  143.                 IF ds=='ILBM' THEN DO
  144.                     destaeformat='IFF-ILBM'
  145.                 END
  146.                 ELSE IF ds=='JPEG' THEN DO
  147.                     destaeformat='JFIF-JPEG'
  148.                 END
  149.                 ELSE DO
  150.                     destaeformat=ds
  151.                 END
  152.                 LOCKGUI
  153.                 LOADPIC 'NAME "' || file || '"'
  154.                 IF destw>0 THEN SCALEPIC destw desth
  155.                 SAVEPIC 'NAME "' || destfile || '" NOFLATTEN EXCLUDEALPHA PLUGIN ' destaeformat
  156.                 CLOSEPIC FORCE
  157.                 UNLOCKGUI
  158.                 ADDRESS
  159.             END /* WHEN */
  160.             WHEN appmode=='PHOTOGENICS2' THEN DO
  161.                 IF ~SHOW('P', 'PHOTOGENICS.1') THEN DO
  162.                     oldstack=PRAGMA('STACK', 25000)
  163.                     ADDRESS COMMAND 'run >NIL: <NIL: Photogenics:Photogenics'
  164.                     ADDRESS COMMAND 'WaitForPort PHOTOGENICS.1'
  165.                     dummy=PRAGMA('STACK', oldstack)
  166.                 END /* IF */
  167.                 ADDRESS PHOTOGENICS.1
  168.                 LOAD '"'file'"'
  169.                 buf=RESULT
  170.                 IF RC=0 THEN DO
  171.                     IF destw>0 THEN SCALE buf destw desth 'SMOOTH'
  172.                     ds=UPPER(destformat)
  173.                     SELECT
  174.                         WHEN ds=='JPEG' THEN DO
  175.                             SAVE buf '"'ds'" "'destfile'" 80'
  176.                         END
  177.                         WHEN ds=='ILBM' THEN DO
  178.                             ADDRESS
  179.                             request('"OK"' 'Saving ILBM with Photogenics might not work yet.')
  180.                             ADDRESS PHOTOGENICS.1
  181.                             IF destcolors>256 THEN DO
  182.                                 destcolors=256
  183.                             END
  184.                             SAVE buf '"'ds'" "'destfile'"' destcolors
  185.                         END
  186.                         WHEN ds=='PNG' THEN DO
  187.                             IF destcolors=16777216 THEN DO
  188.                                 destcolors='24BIT'
  189.                             END
  190.                             IF destcolors>2 & destcolors<16 THEN DO
  191.                                 destcolors=16
  192.                             END
  193.                             IF destcolors>16 & destcolors<256 THEN DO
  194.                                 destcolors=256
  195.                             END
  196.                             SAVE buf '"'ds'" "'destfile'"' destcolors
  197.                         END
  198.                         WHEN ds=='GIF' THEN DO
  199.                             SAVE buf '"'ds'" "'destfile'"'
  200.                         END
  201.                         OTHERWISE NOP
  202.                     END /* SELECT */
  203.                     CLOSE buf
  204.                 END /* IF RC */
  205.                 ELSE DO
  206.                     ADDRESS
  207.                     request('"'getstr(100, 'OK')'"' getstr(4, 'Could not load file »%s«!',file))
  208.                 END /* IF buffer */
  209.                 ADDRESS
  210.             END /* WHEN */
  211.             OTHERWISE NOP
  212.         END /* SELECT */
  213.     END
  214.     ELSE DO
  215.         request('"'getstr(100, 'OK')'"' getstr(6,'File »%s« not found!',file))
  216.     END /* IF EXISTS */
  217. RETURN 0
  218.  
  219. SaveConfig:
  220.     PARSE ARG saveit
  221.     IF OPEN('fh', 'S:ImageConvert.cfg', 'W') THEN DO
  222.         WRITELN('fh', 'ImageConvert')
  223.         WRITELN('fh', appmode)
  224.         WRITELN('fh', reqmode)
  225.         WRITELN('fh', destformat)
  226.         WRITELN('fh', destw)
  227.         WRITELN('fh', desth)
  228.         WRITELN('fh', destcolors)
  229.         CLOSE('fh')
  230. /*
  231.         IF saveit=1 THEN DO
  232.             ADDRESS COMMAND 'COPY <NIL: >NIL: ENV:ImageConvert.cfg ENVARC: QUIET CLONE NOREQ'
  233.         END
  234. */
  235.     END
  236.     ELSE DO
  237.         request('"'getstr(100, 'OK')'"' getstr(7, 'Could not save config file!'))
  238.     END
  239. RETURN 0
  240.  
  241. LoadConfig:
  242.     IF OPEN('fh', 'S:ImageConvert.cfg', 'R') THEN DO
  243.         IF READLN('fh')=='ImageConvert' THEN DO
  244.             appmode=READLN('fh')
  245.             reqmode=READLN('fh')
  246.             destformat=READLN('fh')
  247.             destw=READLN('fh')
  248.             desth=READLN('fh')
  249.             destcolors=READLN('fh')
  250.         END
  251.         CLOSE('fh')
  252.     END
  253.     ELSE DO
  254.         appmode='PPaint'
  255.         reqmode='opus'
  256.         destformat='ILBM'
  257.         destw=640
  258.         desth=480
  259.         destcolors=128
  260.     END
  261. RETURN 0
  262.  
  263. ConfigDialog:
  264.     /* should I ask the user? */
  265.     destc=destcolors
  266.     IF destcolors=16777216 THEN destc='16M'
  267.     /* destw=0 --> do not alter resolution! */
  268.     IF destw~=0 THEN DO
  269.         resolution=destw' x 'desth
  270.     END
  271.     ELSE DO
  272.         resolution=getstr(210, 'As before')
  273.     END
  274.     res=request('"'getstr(102, 'Yes') || '|' || getstr(103, 'No') || '|' || getstr(101, 'Cancel')'"' getstr(200, 'Do you want to convert the image(s) as follows:') || lf || appmode' 'getstr(201, 'to')' 'UPPER(destformat)':'resolution' x 'destc'?')
  275.     IF res=0 THEN EXIT
  276.     /* ask the user */
  277.     IF res=2 THEN DO
  278.         /* reqmode */
  279.         IF reqtools=1 & opus=1 THEN DO
  280.             res=request('"DOpus|ReqTools|' || getstr(101, 'Cancel')'"' getstr(209, 'Which kind of requesters do you like?'))
  281.             IF res=0 THEN EXIT
  282.             IF res=1 THEN reqmode='opus'
  283.             IF res=2 THEN reqmode='reqtools'
  284.         END
  285.  
  286.         /* appmode */
  287.         res=request('"ArtEffect|Photogenics2|PPaint|' || getstr(101, 'Cancel')'"' getstr(202, 'Which application do you want to use?'))
  288.         IF res=0 THEN EXIT
  289.         IF res=1 THEN appmode='ArtEffect'
  290.         IF res=2 THEN appmode='Photogenics2'
  291.         IF res=3 THEN appmode='PPaint'
  292.  
  293.         /* image format */
  294.         formats='ILBM|JPEG|PNG|GIF|'
  295.         IF appmode=='PPaint' THEN formats='ILBM|PNG|GIF|'
  296.         IF appmode=='ArtEffect' THEN formats='ILBM|JPEG|PNG|'
  297.         IF appmode=='Photogenics2' THEN formats='JPEG|PNG|GIF|'
  298.  
  299.         res=request('"'formats || getstr(101, 'Cancel')'"' getstr(206, 'Which formats should the converted image(s) have?'))
  300.         IF res=0 THEN EXIT
  301.         IF appmode=='PPaint' & res>1 THEN res=res+1
  302.         IF appmode=='Photogenics2' THEN res=res+1
  303.         IF res=1 THEN destformat='ilbm'
  304.         IF res=2 THEN destformat='jpeg'
  305.         IF res=3 THEN destformat='png'
  306.         IF res=4 THEN destformat='gif'
  307.  
  308.         /* resolution */
  309.         resw.1=640 ; resh.1=256
  310.         resw.2=640 ; resh.2=480
  311.         resw.3=800 ; resh.3=600
  312.         resw.4=1024 ; resh.4=768
  313.         resw.5=1280 ; resh.5=1024
  314.  
  315.         res=request('"640x256|640x480|800x600|1024x768|1280x1024|'getstr(210, 'As before')'|'getstr(204, 'Custom')'|'getstr(101, 'Cancel')'"' getstr(203, 'Which resolution do you want to use?'))
  316.         IF res=0 THEN EXIT
  317.         IF res=7 THEN DO
  318.             DO UNTIL destw>0
  319.                 destw=numberrequest(getstr(100, 'OK') || '|' || getstr(101, 'Cancel') destw getstr(207, 'How wide should the converted image(s) be (in pixel)?'))
  320.                 IF destw=-1 THEN EXIT
  321.             END
  322.             DO UNTIL desth>0
  323.                 desth=numberrequest(getstr(100, 'OK') || '|' || getstr(101, 'Cancel') desth getstr(208, 'How high should the converted image(s) be (in pixel)?'))
  324.                 IF desth=-1 THEN EXIT
  325.             END
  326.         END
  327.         ELSE IF res=6 THEN DO
  328.             destw=0
  329.         END
  330.         ELSE DO
  331.             destw=resw.res
  332.             desth=resh.res
  333.         END
  334.  
  335.         /* # of colors */
  336.         destcolors=16777216
  337.         IF appmode~=='ArtEffect' & destformat~=='jpeg' THEN DO
  338.             colors='2|4|8|16|32|64|128|256|16M|'
  339.             IF appmode=='PPaint' THEN colors='2|4|8|16|32|64|128|256|'
  340.             destdepth=request('"'colors || getstr(101, 'Cancel')'"' getstr(205, 'How many colors should the converted image(s) have?'))
  341.             IF destdepth=0 THEN EXIT
  342.             IF destdepth=9 THEN destdepth=24
  343.             destcolors=2**destdepth
  344.         END /* IF appmode */
  345.  
  346.         SaveConfig()
  347.     END
  348. RETURN 0
  349.  
  350. newext: PROCEDURE EXPOSE opusport handle
  351.     PARSE ARG destformat ' ' file
  352.     p=LASTPOS('.',file)
  353.     IF p>0 THEN file=LEFT(file,p-1)
  354.     destfile = file || '.' || destformat
  355. RETURN destfile
  356.  
  357. request: PROCEDURE EXPOSE reqmode opusport appname handle
  358.     PARSE ARG '"'buttontext'" ' reqtext
  359.     IF reqmode == 'opus' THEN DO
  360.         ADDRESS VALUE opusport
  361.         DOPUS REQUEST '"'reqtext'" 'buttontext
  362.         res=RC
  363.     END
  364.     ELSE DO
  365.         res=rtezrequest(reqtext, buttontext, appname)
  366.     END
  367. RETURN res
  368.  
  369. numberrequest: PROCEDURE EXPOSE reqmode opusport appname handle
  370.     PARSE ARG buttontext ' ' oldvalue ' ' reqtext
  371.     IF reqmode == 'opus' THEN DO
  372.         ADDRESS VALUE opusport
  373.         DO UNTIL DATATYPE(res)=='NUM'
  374.             DOPUS GETSTRING '"' || reqtext || '" 40' oldvalue buttontext
  375.             res=0
  376.             IF RESULT~=='RESULT' THEN DO
  377.                 res=RESULT
  378.             END
  379.             ELSE DO
  380.                 res=-1
  381.             END
  382.         END
  383.     END
  384.     ELSE DO
  385.         res=rtgetlong(oldvalue, reqtext, appname, buttontext, , buttonres)
  386.         IF buttonres=0 THEN DO
  387.             res=-1
  388.         END
  389.     END
  390. RETURN res
  391.  
  392. getstr: PROCEDURE EXPOSE catalog opusport handle
  393.     PARSE ARG msgno, msgstring
  394.     IF catalog ~= 0 THEN msgstring = GetCatalogStr(catalog,msgno,msgstring)
  395.     DO i = 3 TO ARG()
  396.         PARSE VAR msgstring fore '%s' aft
  397.         msgstring = fore||ARG(i)||aft
  398.     END
  399. RETURN msgstring
  400.  
  401. Initialize:
  402.     appname='ImageConvert'
  403.     lf='0a'x
  404.  
  405.     catalog=0
  406.     locale=1
  407.     IF ~SHOW('L', 'locale.library') THEN DO
  408.         IF ~ADDLIB('locale.library',0,-30,0) THEN DO
  409.             locale=0
  410.         END
  411.     END /* IF */
  412.  
  413.     IF locale=1 THEN catalog = OpenCatalog(appname'.catalog','english',2)
  414.  
  415.     IF ~SHOW('L','rexxsupport.library') THEN DO
  416.         IF ~ADDLIB('rexxsupport.library',0,-30,0) THEN DO
  417.             SAY GetStr(1,'Didnt find rexxsupport.library!')
  418.             EXIT
  419.         END
  420.     END /* IF */
  421.  
  422.     LoadConfig()
  423.  
  424.     opus=0
  425.     IF SHOW('P', 'DOPUS.1') THEN DO
  426.         opusport='DOPUS.1'
  427.         opus=1
  428.     END
  429.     ELSE DO
  430.         reqmode='reqtools'
  431.     END /* IF */
  432.  
  433.     reqtools=1
  434.     IF ~SHOW('L', 'rexxreqtools.library') THEN DO
  435.         IF ~ADDLIB('rexxreqtools.library', 0, -30, 0) THEN DO
  436.             reqtools=0
  437.             IF reqmode='reqtools' THEN DO
  438.                 IF opus=1 THEN DO
  439.                     reqmode='opus'
  440.                 END
  441.                 ELSE DO
  442.                     SAY GetStr(2, 'Didnt find DOpus or RexxReqTools for displaying requesters!')
  443.                 END
  444.             END
  445.         END
  446.     END /* IF */
  447.  
  448.     IF files=='' THEN DO
  449.         request('"'getstr(100, 'OK')'"' getstr(3,'I need some filenames of pictures to convert!'))
  450.         EXIT
  451.     END /* IF */
  452.  
  453.     /* no progress bar opened! */
  454.  
  455.     handle=0
  456. RETURN 0
  457.  
  458.